<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>权限配置管理</title>
	<#include "./comm/meta-base.ftl" />
	<#include "./comm/meta-easyui.ftl" />
	<#include "./comm/meta-comm.ftl" />
	<link href="${basePath}/static/css/privilege.css" rel="stylesheet" />
</head>
<body>
   		<div id="datagrid_toolbar">
            <div class="buttons-preview" >
                <a class="btn btn-default  btn-sm" 
                	href="javascript:addTreeNode()"><i class="fa fa-plus"></i>新增节点</a>
                <a class="btn btn-primary btn-sm" 
                	href="javascript:modifyTreeNode()"><i class="fa fa-edit"></i> 修改节点</a>
                <a class="btn btn-danger btn-sm" 
                	href="javascript:removeTreeNode()"><i class="fa fa-times"></i>删除节点</a>
               
            </div>
        </div>
	    <table id="data_table" class="easyui-treegrid"  border="0"  rownumbers="true" fitColumns="true" fit="true"
               data-options="
                   url: '${basePath}/company/company-tree-json',
                   method: 'get',
                   rownumbers: true,
                   idField: 'id',
                   lines: true,
                   toolbar:'#datagrid_toolbar',
                   treeField: 'text'
               ">
           <thead>
           <tr>
                <th data-options="field:'text',width:300">公司组织树</th>
                <th data-options="field:'id',width:width(0.1),formatter:operateFormatter">操 作</th>
            </tr>
           </thead>
       </table>


</body>
</html>
<script type="text/javascript">
function operatePriFormatter(value,row,index){
	var html="";
	if(''!=value&&null!=value){
		var operateJSON = eval("(" + value + ")"); 
		for(var i=0;i<operateJSON.length;i++){
			var operateObj=operateJSON[i];
			html+="<div value='"+operateObj.operateId+"' class='operateDiv ' >"+operateObj.name+"|"+operateObj.code+"</div>";
		}
	}
	return html;
}

function removeTreeNode(){
	var row = $('#data_table').datagrid('getSelected');
	var url="";
	if(row.type=='ROOT'){//根节点，不可修改
		Notify('根节点不可删除!', 'top-right', '5000', 'danger', 'fa-bolt', true);
	}
	if(row.type=='COMPANY'){//
		url="${basePath}/company/company-remove/"+row.id;
		bootbox.confirm("你确定要删除"+row.text+"?", function (result) {
        	if (result) {
            	$.post(url,{},function(data){
					if(data.success){
            			window.location.reload();
            		}else{
            			Notify(row.text+'已经被使用,不可删除!', 'top-right', '5000', 'danger', 'fa-bolt', true);
            		}
				},"json");
         	}
        });
	}
	if(row.type=='DEPARTMENT'){//权限节点 
		url="${basePath}/department/department-remove/"+row.id;
		bootbox.confirm("你确定要删除"+row.text+"?", function (result) {
        	if (result) {
            	$.post(url,{},function(data){
            		if(data.success){
            			window.location.reload();
            		}else{
            			Notify(row.text+'已经被使用,不可删除!', 'top-right', '5000', 'danger', 'fa-bolt', true);
            		}
				},"json");
         	}
        });
	}
	
}

function operateFormatter(value, row, index){
	var html="";
	if(row.type!='ROOT'){
		html+="&nbsp;&nbsp;<a class=\"btn btn-success btn-xs icon-only white\" href=\"javascript:moveUp('"+value+"','"+row.type+"')\"><i class=\"fa  fa-arrow-up\"></i></a>";
		html+="&nbsp;&nbsp;<a class=\"btn btn-danger  btn-xs icon-only white\" href=\"javascript:moveDown('"+value+"','"+row.type+"')\"><i class=\"fa fa-arrow-down\"></i></a>";
	}
	return html;
}
function moveUp(id,type){
	var url="";
	if(type=='COMPANY'){
		url="${basePath}/company/moveup/"+id;
	}
	if(type=='DEPARTMENT'){
		url="${basePath}/department/moveup/"+id;
	}

	$.post(url,{},function(data){
	  	window.location.reload();
	});
}
function moveDown(id,type){
	var url="";
	if(type=='COMPANY'){
		url="${basePath}/company/movedown/"+id;
	}
	if(type=='DEPARTMENT'){
		url="${basePath}/department/movedown/"+id;
	}

	$.post(url,{},function(data){
	   window.location.reload();
	});
}

function modifyTreeNode(){
	var row = $('#data_table').datagrid('getSelected');
	var url="";
	var winName="";
	if(row.type=='ROOT'){
		Notify('根节点不可被编辑!', 'top-right', '5000', 'danger', 'fa-bolt', true);
	}
	if(row.type=='COMPANY'){
		url="${basePath}/company/company-modifyin/"+row.id;
		winName="修改公司";
		openWindow(winName,url,900,600,true);
	}
	if(row.type=='DEPARTMENT'){
		url="${basePath}/department/department-modifyin/"+row.id;
		winName="修改部门";
		openWindow(winName,url,900,600,true);
	}
	
}

function addTreeNode(){
	var row = $('#data_table').datagrid('getSelected');
	var url="";
	var winName="";
	if(row.type=='ROOT'){
		url="${basePath}/company/company-addin";
		winName="添加公司";
	}
	if(row.type=='COMPANY'){
		url="${basePath}/department/department-addin?id="+row.id;
		winName="添加部门";
	}
	if(row.type=='DEPARTMENT'){
		Notify('不可添加子部门!', 'top-right', '5000', 'danger', 'fa-bolt', true);
	}
	openWindow(winName,url,900,600,true);
}


$(document).ready(function(){
	
});
</script>


